package com.lestar.htmlparser.jiuyou;

import com.lestar.htmlparser.BaseParser;
import com.lestar.htmlparser.DeepFilter;
import com.lestar.htmlparser.model.NewGame;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.HasChildFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.htmlparser.util.SimpleNodeIterator;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Ray on 2016/10/11.
 */

public class NewGameParser extends BaseParser<NewGame> {

    @Override
    public List<NewGame> parseList(String source) {
        try {
            Parser parser = new Parser(source, getFeedback());
            parser.setEncoding("utf-8");

            //find root
            DeepFilter deepFilter =
                    DeepFilter.init()
                            .deepMatch(DeepFilter.Builder.getTagFilter().name("div").attribute("class", "main-con").create())
                            .deeper()
                            .match(DeepFilter.Builder.getTagFilter().name("div").attribute("class", "box hope-rank-con").create())
                            .deeper()
                            .match(DeepFilter.Builder.getTagFilter().name("div").attribute("class", "box-text").create())
                            .deeper()
                            .match(DeepFilter.Builder.getTagFilter().name("table").create())
                            .create();
            NodeList allNodes = parser.parse(null);
            findNode(allNodes, deepFilter);
            Node root = temp;

            //find items
            NodeFilter itemFilter = new HasChildFilter(new AndFilter(new TagNameFilter("td"), new HasAttributeFilter("class", "static")));
            NodeList itemList = root.getChildren().extractAllNodesThatMatch(itemFilter);

            //parse items
            ArrayList<NewGame> newGames = new ArrayList<>();
            SimpleNodeIterator iterator = itemList.elements();
            if (!iterator.hasMoreNodes()) {
                return newGames;
            }
            Node node = iterator.nextNode();
            for (; iterator.hasMoreNodes(); node = iterator.nextNode()) {
                NewGame newGame = new NewGame();
                NodeList childNodes = node.getChildren();

                //get rank
                DeepFilter rankFilter =
                        DeepFilter.init()
                                .match(DeepFilter.Builder.getTagFilter().name("td").attributeRegex("class", "num \\w*").create())
                                .deeper()
                                .match(DeepFilter.Builder.getTagFilter().name("span").create())
                                .create();
                findNode(childNodes, rankFilter);
                Tag rank = (Tag) temp;
                newGame.setRank(Integer.parseInt(rank.toPlainTextString()));

                //get name
                DeepFilter nameFilter =
                        DeepFilter.init()
                                .match(DeepFilter.Builder.getTagFilter().name("td").attribute("class", "name").create())
                                .deeper()
                                .match(DeepFilter.Builder.getTagFilter().name("a").create())
                                .create();
                findNode(childNodes, nameFilter);
                Tag name = (Tag) temp;
                newGame.setName(name.toPlainTextString());

                //get static
                DeepFilter staticFilter =
                        DeepFilter.init()
                                .match(DeepFilter.Builder.getTagFilter().name("td").attribute("class", "static").create())
                                .deeper()
                                .match(DeepFilter.Builder.getTagFilter().name("span").create())
                                .create();
                findNode(childNodes, staticFilter);
                Tag staticTag = (Tag) temp;
                newGame.setStatistic(staticTag.toPlainTextString());

                //get type
                DeepFilter typeFilter =
                        DeepFilter.init()
                                .match(DeepFilter.Builder.getTagFilter().name("td").attribute("class", "type").create())
                                .create();
                findNode(childNodes, typeFilter);
                Tag type = (Tag) temp;
                newGame.setType(type.toPlainTextString());

                //get hot
                DeepFilter hotFilter =
                        DeepFilter.init()
                                .match(DeepFilter.Builder.getTagFilter().name("td").attribute("class", "hottr hot").create())
                                .deeper()
                                .match(DeepFilter.Builder.getTagFilter().name("span").create())
                                .create();
                findNode(childNodes, hotFilter);
                Tag hot = (Tag) temp;
                newGame.setHot(Integer.parseInt(hot.toPlainTextString()));

                newGames.add(newGame);
            }
            return newGames;
        } catch (ParserException e) {
            e.printStackTrace();
        }
        return null;
    }
}
